import axios from 'axios'
import {SEQUENTIAL_PLAY, 
        RANDOM_PLAY, 
        LOOP_PALY, 
        SET_MUSIC_LIST, 
        CURRENTMUSIC ,
        CURRENTLYRIC,
        SETPLAYING, 
        SETAUDIO
        } from './action-types'
import parseLyric from '../Utils/parseLyric'

//设置播放列表
const setMusicList = (data)=>({type:SET_MUSIC_LIST,data:data})

//异步获取播放列表
export const setMusicListAsync = ()=>{
    return dispatch=>{
        var p =axios.get('/music/music-list.json')
        p.then(data=>{
            console.log(data.data.list)
            dispatch(setMusicList(data.data.list))
        })

    }
}

//设置顺序模式
export const setSequentialMode = ()=>({type:SEQUENTIAL_PLAY})
//设置顺序模式
export const setRandomMode = ()=>({type:RANDOM_PLAY})
//设置顺序模式
export const setLoopMode = ()=>({type:LOOP_PALY})
//设置当前播放的歌曲
export const setCurrentMusic = (index)=>({type:CURRENTMUSIC,data:index})

//设置当前歌曲歌词
const setLyricList=(lyric)=>({type:CURRENTLYRIC,data:lyric})

//异步获取歌词列表
export const setLyricListAsync = (musicName)=>{
    console.log('setLyricListAsync=>',musicName)
    return dispatch=>{
        var p =axios.get(`/music/${musicName}.lrc`)
        p.then(data=>{
            console.log(parseLyric(data.data))
             dispatch(setLyricList(parseLyric(data.data)))
        })

    }
}

//设置是否正在播放
export const setPlaying =(data)=>({type:SETPLAYING,data})

//设置播放对象
export const setAudio =(data)=>({type:SETAUDIO,data})